package com.sys.web.head.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.servlet.BaseServlet;
import com.sys.common.util.PageInfoUtil;
import com.sys.orders.pojo.TOrders;
import com.sys.orders.pojo.TOrdersGoods;
import com.sys.orders.service.IOrdersDbService;
import com.sys.orders.service.impl.OrdersDbServiceImpl;
import com.sys.users.pojo.TCart;
import com.sys.users.pojo.TUsers;
import com.sys.users.pojo.TUsersEnum;
import com.sys.users.service.IUsersDbService;
import com.sys.users.service.impl.UsersDbServiceImpl;
import com.sys.wares.service.IGoodsDbService;
import com.sys.wares.service.impl.GoodsDbServiceImpl;

/**
 * 订单操作
 * 
 * @author 杨志超
 */
@WebServlet(urlPatterns = "/head/OrdersHeadServlet")
public class OrdersHeadServlet extends BaseServlet
{
	@Resource
	private IOrdersDbService ordersDbService;
	@Resource
	private IGoodsDbService goodsDbService ; 
	@Resource
	private IUsersDbService usersDbService ; 
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.usersDbService = new UsersDbServiceImpl();
		this.ordersDbService = new OrdersDbServiceImpl();
		this.goodsDbService = new GoodsDbServiceImpl() ;
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("ordersList".equalsIgnoreCase(method))
		{
			/* ordersList */
			ordersList(request, response);
		} else if ("ordersInsert".equalsIgnoreCase(method))
		{
			/* ordersInsert */
			ordersInsert(request, response);
		}else if ("ordersInsertSubmit".equalsIgnoreCase(method))
		{
			/* ordersInsertSubmit */
			ordersInsertSubmit(request, response);
		} else if ("ordersGoodsUpdate".equalsIgnoreCase(method))
		{
			/* ordersGoodsUpdate */
			ordersGoodsUpdate(request, response);
		}else if ("ordersGoodsList".equalsIgnoreCase(method))
		{
			/* ordersGoodsList */
			ordersGoodsList(request, response);
		} else if ("ordersGoodsUpdate".equalsIgnoreCase(method))
		{
			/* ordersGoodsUpdate */
			ordersGoodsUpdate(request, response);
		}
	}
	
	/**
	 * 订单列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String ordersList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		TUsers users = (TUsers) this.findObjfromSession(request, "users");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("usersId", users.getId());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<TOrders> apiResponse = this.ordersDbService.findCondListOrdersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/orders/ordersList";
	}

	/**
	 * 打开下单页面
	 * @param request
	 * @param response
	 */
	public String ordersInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		TUsers users = (TUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("usersId", users.getId());
		condMap.put("status", TUsersEnum.STATUS_ENABLE.getStatus());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<TCart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/orders/ordersInsert" ; 
	}
	
	/**
	 * 下单提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String ordersInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		TUsers users = (TUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("usersId", users.getId());
		condMap.put("status", TUsersEnum.STATUS_ENABLE.getStatus());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<TCart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 接收必要信息 */
		String receName = request.getParameter("receName");
		String recePhone = request.getParameter("recePhone");
		String receAddress = request.getParameter("receAddress");
		String content = request.getParameter("content");
		
		/* 创建对象 */
		TOrders orders = new TOrders();
		orders.setUsersId(users.getId());
		orders.setReceName(receName);
		orders.setRecePhone(recePhone);
		orders.setReceAddress(receAddress);
		orders.setContent(content);
		
		/* 设置购物车 */
		List<TCart> cartList = apiResponse.getDataListJava();
		orders.setCartList(cartList);
		
		ApiResponse<Object> apiResponseDb = this.ordersDbService.saveOneOrdersService(orders);
		if(apiResponseDb.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			return "redirect:/head/orders/ordersList.htm" ; 
		}
		return this.ordersInsert(request, response);
	}

	/**
	 * 订单更新
	 * @param request
	 * @param response
	 */
	public String ordersUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<TOrders> apiResponse = this.ordersDbService.findOneOrdersService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 同时查询出订单商品信息 */
		TOrders orders = apiResponse.getDataOneJava();
		condMap.clear();
		condMap.put("ordersId", orders.getId());
		condMap.put("extend", "true");
		ApiResponse<TOrdersGoods> ordersGoodsApiResponse = this.ordersDbService.findCondListOrdersGoodsService(null, condMap);
		request.setAttribute("ordersGoodsResponse", ordersGoodsApiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/head/orders/ordersUpdate" ; 
		}
		/* 页面跳转 */
		return "/head/orders/ordersInfo" ; 
	}

	/**
	 * 订单商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String ordersGoodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String ordersId = request.getParameter("ordersId");
		if(ordersId == null)
		{
			ordersId = "" ; 
		}
		request.setAttribute("ordersId", ordersId);
		condMap.put("ordersId", ordersId);
		
		/* 扩展信息 */
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<TOrdersGoods> apiResponse = this.ordersDbService.findCondListOrdersGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/orders/ordersGoodsList" ; 
	}

	/**
	 * 订单商品更新
	 * @param request
	 * @param response
	 */
	public String ordersGoodsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<TOrdersGoods> apiResponse = this.ordersDbService.findOneOrdersGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/head/orders/ordersGoodsUpdate" ; 
		}
		/* 页面跳转 */
		return "/head/orders/ordersGoodsInfo" ; 
	}
}
